//
//  PMControl.h
//
//  Copyright (C) 2007 Visual.Wei
//  Author: Wei Shuai <cpuwolf@sina.com>
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//

#include <windows.h>
#include "progbar.h"
#include "PMListView.h"
#include "PMTabCtrl.h"
#include "PMSplitCtrl.h"

extern PMTabCtrl * p_pmtc;
extern HIMAGELIST g_ImgList;
extern PMSplitCtrl * p_pmsplit;

//------------------------------------------------------------------------------------------------
HTREEITEM PMListView::AddChild(HTREEITEM parent,TCHAR * txt,int len)
{
	TVINSERTSTRUCT insert;
	if(!parent)
		insert.hParent=hroot;
	else
		insert.hParent=parent;
	insert.hInsertAfter=TVI_ROOT;
	memset(&insert.item,0,sizeof(insert.item));
	insert.item.mask=TVIF_TEXT|TVIF_STATE|TVIF_SELECTEDIMAGE|TVIF_IMAGE;
	insert.item.pszText=txt;
	insert.item.cchTextMax=len;
	//insert.item.state=TVIS_EXPANDED;
	insert.item.stateMask=TVIS_EXPANDED;
	insert.item.iImage=1;
	insert.item.iSelectedImage=1;
	return TreeView_InsertItem(g_hwnd,&insert);
}
HTREEITEM PMListView::AddChild(HTREEITEM parent,CEDevice * pdev,TCHAR * txt,int len)
{
	TVINSERTSTRUCT insert;
	if(!parent)
		insert.hParent=hroot;
	else
		insert.hParent=parent;

	insert.hInsertAfter=TVI_ROOT;
	memset(&insert.item,0,sizeof(insert.item));
	insert.item.mask=TVIF_TEXT|TVIF_STATE|TVIF_PARAM|TVIF_SELECTEDIMAGE|TVIF_IMAGE;
	insert.item.pszText=txt;
	insert.item.cchTextMax=len;
	//insert.item.state=TVIS_EXPANDED;
	insert.item.stateMask=TVIS_EXPANDED;
	if((pdev)&&(pdev->type==CEDT_INTERFACE))
	{
		insert.item.iImage=1;
		insert.item.iSelectedImage=1;
	}
	else
	{
		insert.item.iImage=3;
		insert.item.iSelectedImage=3;
	}

	insert.item.lParam=(LPARAM)pdev;
	return TreeView_InsertItem(g_hwnd,&insert);
}
void PMListView::DeleteChild(TVITEM * pitem)
{
	if(pitem)
		if(pitem->mask|TVIF_PARAM)
			if(pitem->lParam)
				free((void *)(pitem->lParam));
}
void PMListView::SelectChange(LPNMTREEVIEW tv)
{
	TVITEM * pitem=&tv->itemNew;
	CEDevice * poldDevice=g_pCurDevice;
	TabPage * poldtp=NULL;
	g_pCurDevice=NULL;

	if(poldDevice)
		poldtp=poldDevice->pTPheader;
	
	if(pitem)
		if(pitem->mask|TVIF_PARAM)
			if(pitem->lParam)
			{
				g_pCurDevice=(CEDevice *)pitem->lParam;			
			}

	if(g_pCurDevice!=poldDevice)
	{
		if(!((g_pCurDevice)&&(poldtp==g_pCurDevice->pTPheader)))
			p_pmtc->ChangeView();
		p_pmtc->RefreshPage();
	}
}
DWORD PMListView::Notify(int idCtrl,LPNMHDR pnmh)
{
	LPNMTREEVIEW tv;
	tv=(LPNMTREEVIEW)pnmh;
	switch(pnmh->code)
	{
		case TVN_SELCHANGED:
			SelectChange(tv);
			break;
		case TVN_DELETEITEM:
			DeleteChild(&tv->itemOld);
			break;
	}
	return TRUE;
}
DWORD PMListView::ReSize()
{
	RECT rect;
	GetClientRect(g_hpwnd,&rect);
	//SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, FALSE);	
	//rect.top+=5;
	MoveWindow(g_hwnd,rect.left,rect.top,rect.right-rect.left,p_pmsplit->WndRect.top-rect.top,true);
	return true;
}

PMListView::PMListView(HWND hWnd):PMControl()
{
	RECT rect;
	//|TVS_LINESATROOT
	GetClientRect(hWnd,&rect);
	g_hwnd = CreateWindowEx ( WS_EX_CLIENTEDGE,  WC_TREEVIEW, TEXT("Tree View"),
	                           WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES|TVS_HASBUTTONS,
	                           0, rect.top,rect.right,rect.bottom/2, hWnd,(HMENU)NULL,g_hInst, NULL);

	TreeView_SetImageList(g_hwnd,g_ImgList,TVSIL_NORMAL);

	TVINSERTSTRUCT insert;
	insert.hParent=TVI_ROOT;
	insert.hInsertAfter=TVI_ROOT;
	memset(&insert.item,0,sizeof(insert.item));
	insert.item.mask=TVIF_TEXT|TVIF_STATE|TVIF_IMAGE;
	insert.item.pszText=_T("WinCE Device");
	insert.item.cchTextMax=20*sizeof(TCHAR);
	insert.item.state=TVIS_EXPANDED;
	insert.item.stateMask=TVIS_EXPANDED|TVIS_OVERLAYMASK;
	insert.item.iImage=0;
	insert.item.iSelectedImage=0;
	hroot=TreeView_InsertItem(g_hwnd,&insert);
	
	Update();
}
//------------------------------------------------